a tool for shared writing and social publishing
1import { z } from "zod";
2import { makeRoute } from "../lib";
3import type { Env } from "./route";
4
5export type GetLeafletDataReturnType = Awaited<
6 ReturnType<(typeof get_leaflet_data)["handler"]>
7>;
8
9const leaflets_in_publications_query = `leaflets_in_publications(*, publications(*), documents(*))`;
10export const get_leaflet_data = makeRoute({
11 route: "get_leaflet_data",
12 input: z.object({
13 token_id: z.string(),
14 }),
15
16 handler: async ({ token_id }, { supabase }: Pick<Env, "supabase">) => {
17 let res = await supabase
18 .from("permission_tokens")
19 .select(
20 `*,
21 permission_token_rights(*, entity_sets(permission_tokens(${leaflets_in_publications_query}))),
22 custom_domain_routes!custom_domain_routes_edit_permission_token_fkey(*),
23 ${leaflets_in_publications_query}`,
24 )
25 .eq("id", token_id)
26 .single();
27
28 type t = typeof res;
29 type data = Exclude<t["data"], null>;
30
31 //All of these shenanigans are to make entity_set optional so that we don't have to write it when we create this data elsewhere, mainly in LeafletList
32 return {
33 result: res as Omit<t, "data"> & {
34 data:
35 | null
36 | (Omit<data, "permission_token_rights"> & {
37 permission_token_rights: (Omit<
38 data["permission_token_rights"][0],
39 "entity_sets"
40 > & {
41 entity_sets?: data["permission_token_rights"][0]["entity_sets"];
42 })[];
43 });
44 },
45 };
46 },
47});